package com.orange.adminapi.controller.receipt;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.orange.core.pojo.PageReqDto;
import com.orange.core.pojo.R;
import com.orange.core.pojo.entity.receipt.Receipt;
import com.orange.core.pojo.req.receipt.ReceiptAuditReq;
import com.orange.core.pojo.req.receipt.ReceiptPageReq;
import com.orange.core.service.receipt.ReceiptService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 小票
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/receipt")
public class ReceiptController {

    private final ReceiptService receiptService;

    /**
     * 分页查询
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).RECEIPT_LIST.permission.identifier)")
    @PostMapping("/page")
    public R<Page<Receipt>> page(@RequestBody PageReqDto<ReceiptPageReq> req) {
        if (req.getCondition() == null) {
            req.setCondition(new ReceiptPageReq());
        }
        return R.ok(receiptService.page(req));
    }

    /**
     * 详情
     */
    @GetMapping("/info/{id}")
    public R<Receipt> info(@PathVariable("id") int id) {
        return R.ok(receiptService.info(id));
    }

    /**
     * 审核
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).RECEIPT_AUDIT.permission.identifier)")
    @PostMapping("/audit/{id}")
    public R<Boolean> audit(@PathVariable int id, @RequestBody ReceiptAuditReq req) {
        receiptService.audit(id, req);
        return R.ok();
    }
}
